﻿using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using ERPSystem.Common.Domains.supplier;
using System.Linq;
using SqlSugar;

namespace BasicInformationCommon.API.BasicInformationCommons.Suppliers
{
    public class PurchaseOrderHandler : IRequestHandler<PurchaseOrderCommand, Result<PageResultDto<PurchaseOrderDTO>>>
    {
        private readonly IBaseRepository<PurchaseOrder> purchaseOrderRe;
        private readonly ISqlSugarClient sqlSugarClient;

        public PurchaseOrderHandler(IBaseRepository<PurchaseOrder> purchaseOrderRe, ISqlSugarClient sqlSugarClient)
        {
            this.purchaseOrderRe = purchaseOrderRe;
            this.sqlSugarClient = sqlSugarClient;
        }

        public async Task<Result<PageResultDto<PurchaseOrderDTO>>> Handle(PurchaseOrderCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<PurchaseOrderDTO>>();
            var page = new PageResultDto<PurchaseOrderDTO>() { TotalCount = 0, TotalPage = 0, List = new List<PurchaseOrderDTO>() };
            try
            {
                RefAsync<int> totalCount = 0;
                RefAsync<int> totalPage = 0;
                var list = sqlSugarClient.Queryable<PurchaseOrder>().Select(c => new PurchaseOrderDTO()
                {
                    PurchaseOrderId= c.PurchaseOrderId,
                    ReceiptNum = c.ReceiptNum,
                    ReceiptState = c.ReceiptState,
                    ReceiptDate = c.ReceiptDate,
                    BusinessType = c.BusinessType,
                    ServiceStatus = c.ServiceStatus,
                    Department = c.Department,
                    Buyer = c.Buyer,
                    Operator = c.Operator,
                    Auditor = c.Auditor,
                    ApprovalDate = c.ApprovalDate,
                    MaterialDemand = c.MaterialDemand,
                    DeliveryDate = c.DeliveryDate,
                    System = c.System,
                    PurchasingContract = c.PurchasingContract,
                    Remark = c.Remark,
                    OrderNumber = c.OrderNumber,
                    MaterialCode = c.MaterialCode,
                    MaterialName = c.MaterialName,
                    TypeSpecification = c.TypeSpecification,
                    Color = c.Color,
                    DocumentType = c.DocumentType,
                    RequirementDate = c.RequirementDate,
                    Quantity = c.Quantity,
                    UnitPrice = c.UnitPrice,
                    Amount = c.Amount,
                    ArrivalQuantity = c.ArrivalQuantity,
                    PurchaseDescription = c.PurchaseDescription,
                });
                var res = await list.ToPageListAsync(request.PageIndex, request.PageSize, totalCount, totalPage);
                page.TotalCount = totalCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取客户档案信息成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

        public async Task<Result<PageResultDto<PurchaseOrderDTO>>> HandleFan(PurchaseOrderCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<PurchaseOrderDTO>>();
            try
            {
                var list = sqlSugarClient.Queryable<PurchaseOrder>().Select(c => new PurchaseOrderDTO()
                {
                    PurchaseOrderId = c.PurchaseOrderId,
                    ReceiptNum = c.ReceiptNum,
                    ReceiptState = c.ReceiptState,
                    ReceiptDate = c.ReceiptDate,
                    BusinessType = c.BusinessType,
                    ServiceStatus = c.ServiceStatus,
                    Department = c.Department,
                    Buyer = c.Buyer,
                    Operator = c.Operator,
                    Auditor = c.Auditor,
                    ApprovalDate = c.ApprovalDate,
                    MaterialDemand = c.MaterialDemand,
                    DeliveryDate = c.DeliveryDate,
                    System = c.System,
                    PurchasingContract = c.PurchasingContract,
                    Remark = c.Remark,
                    OrderNumber = c.OrderNumber,
                    MaterialCode = c.MaterialCode,
                    MaterialName = c.MaterialName,
                    TypeSpecification = c.TypeSpecification,
                    Color = c.Color,
                    DocumentType = c.DocumentType,
                    RequirementDate = c.RequirementDate,
                    Quantity = c.Quantity,
                    UnitPrice = c.UnitPrice,
                    Amount = c.Amount,
                    ArrivalQuantity = c.ArrivalQuantity,
                    PurchaseDescription = c.PurchaseDescription,
                });
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

    }
}
